package com.example.first_work;

import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentTransaction;

import android.view.View;
import android.view.Window;
import android.os.Bundle;
import android.widget.ImageButton;
import android.widget.LinearLayout;

public class MainActivity extends AppCompatActivity implements View.OnClickListener{
    private Fragment mtab01=new weixinFragment();
    private Fragment mtab02=new frdFragment();
    private Fragment mtab03=new contactFragment();
    private Fragment mtab04=new settingsFragment();
    private FragmentManager fm;

    private  LinearLayout mtabweixin;
    private  LinearLayout mtabfrd;
    private  LinearLayout mtabaddress;
    private  LinearLayout mtabsettings;
    private  ImageButton mimgweixin;
    private  ImageButton mimgfrd;
    private  ImageButton mimgaddress;
    private  ImageButton mimgsettings;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initView();
        initFragment();
        Listening();
        selectfragment(0);
    }

    private void initFragment(){
        fm = getSupportFragmentManager();
        FragmentTransaction transaction = fm.beginTransaction();
        transaction.add(R.id.id_content,mtab01);
        transaction.add(R.id.id_content,mtab02);
        transaction.add(R.id.id_content,mtab03);
        transaction.add(R.id.id_content,mtab04);
        transaction.commit();
    }

    private void initView(){
        mtabweixin=(LinearLayout) findViewById(R.id.id_lab_weixin);
        mtabfrd=(LinearLayout) findViewById(R.id.id_lab_frd);
        mtabaddress=(LinearLayout) findViewById(R.id.id_lab_contact);
        mtabsettings=(LinearLayout) findViewById(R.id.id_lab_settings);

        mimgweixin=findViewById(R.id.id_lab_weixin_img);
        mimgfrd=findViewById(R.id.id_lab_frd_img);
        mimgaddress=findViewById(R.id.id_lab_contact_img);
        mimgsettings=findViewById(R.id.id_lab_settings_img);
    }

    private void hidefragment(FragmentTransaction transaction){
        transaction.hide(mtab01);
        transaction.hide(mtab02);
        transaction.hide(mtab03);
        transaction.hide(mtab04);
        transaction.commit();
    }

    private void selectfragment(int i){
        FragmentTransaction transaction = fm.beginTransaction();
        hidefragment(transaction);
        resetimg();
        switch (i){
            case 0:
                transaction.show(mtab01);
                mimgweixin.setImageResource(R.drawable.tab_weixin_pressed);
                break;
            case 1:
                transaction.show(mtab02);
                mimgfrd.setImageResource(R.drawable.tab_find_frd_pressed);
                break;
            case 2:
                transaction.show(mtab03);
                mimgaddress.setImageResource(R.drawable.tab_address_pressed);
                break;
            case 3:
                transaction.show(mtab04);
                mimgsettings.setImageResource(R.drawable.tab_settings_pressed);
                break;
            default:
                break;
        }
    }

    @Override
    public void onClick(View v){

        switch (v.getId()) {
            case R.id.id_lab_weixin_img:
                selectfragment(0);
                break;
            case R.id.id_lab_frd_img:
                selectfragment(1);
                break;
            case R.id.id_lab_contact_img:
                selectfragment(2);
                break;
            case R.id.id_lab_settings_img:
                selectfragment(3);
                break;
            default:
                break;
        }
    }

    public  void  resetimg(){
        mimgweixin.setImageResource(R.drawable.tab_weixin_normal);
        mimgfrd.setImageResource(R.drawable.tab_find_frd_normal);
        mimgaddress.setImageResource(R.drawable.tab_address_normal);
        mimgsettings.setImageResource(R.drawable.tab_settings_normal);
    }

    private  void Listening(){
        mtabweixin.setOnClickListener(this);
        mtabfrd.setOnClickListener(this);
        mtabaddress.setOnClickListener(this);
        mtabsettings.setOnClickListener(this);
    }
}